package cn.com.cetccst.basiccollection;

import android.content.Context;

import java.util.HashMap;
import java.util.Map;

import cn.com.cetccst.basiccollection.basicinfo.BasicInfoUtil;
import cn.com.cetccst.basiccollection.peroid.PeroidCollectHelper;
import cn.com.cetccst.datacenter.collection.CollectionRepository;
import cn.com.cetccst.datacenter.collection.entity.CollectionConfigEntity;

public class BasicInfoEngine {
    public static final String TAG = "StaticInfoEngine";
    private static BasicInfoEngine INSTANCE;
    private Context mContext;
    private IBasicInfoOwner mStaticInfoOwner;
    private PeroidCollectHelper mPeroidCollectHelper;
    private Map<InfoType, Collector> mCollectors = new HashMap<>();


    public static void initialize(Context context) {
        if (INSTANCE == null) {
            INSTANCE = new BasicInfoEngine(context);
        }
    }

    public static BasicInfoEngine getInstance() {
        if (INSTANCE == null) {
            throw new RuntimeException("init first");
        }
        return INSTANCE;
    }

    private BasicInfoEngine(Context context) {
        mContext = context;
        mStaticInfoOwner = initInfoOwner();
        mPeroidCollectHelper = new PeroidCollectHelper(context);

        initCollectors();
        CollectionConfigEntity config = CollectionRepository.getInstance().getCollectionConfigDao().getConfig();
        peroidCollect(config);

        BasicInfoUtil.printOSInfo(context);
    }

    private IBasicInfoOwner initInfoOwner() {
        return new MockDeviceInfoOwner(mContext);
    }

    public void peroidCollect(CollectionConfigEntity config) {
        mPeroidCollectHelper.startWorker(config);
    }

    private void initCollectors() {
        mCollectors.put(InfoType.HARDWARE_INFO, new Collector(mStaticInfoOwner, InfoType.HARDWARE_INFO));
        mCollectors.put(InfoType.SOURCE_INFO, new Collector(mStaticInfoOwner, InfoType.SOURCE_INFO));
    }

    public Collector getCollector(InfoType type) {
        return mCollectors.get(type);
    }
}
